//
//  HNSH_ AccountViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/28.
//

#import "HNSH_AccountViewController.h"

//view
#import "HNSH_AccountCell.h"
#import "HNSH_AccountHeaderView.h"

//vc
#import "HNSH_AccountEditViewController.h"          //个人信息
#import "HNSH_AccountSafeViewController.h"          //账户安全
#import "HNSH_AboutUsViewController.h"              //关于我们
#import "HNSH_FeedbackViewController.h"

@interface HNSH_AccountViewController ()

/* infoAry */
@property (nonatomic, strong) NSArray *infoAry;

/* addBtn */
@property (nonatomic, strong) UIButton *addBtn;

/* headerView */
@property (nonatomic, strong) HNSH_AccountHeaderView *headerView;

@end

static NSString *HNSH_AccountCellID = @"HNSH_AccountCellID";

@implementation HNSH_AccountViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.headerView) {
        self.headerView.userInfo = [ST_UserManager shareInstance].user;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"账户管理" showLeftItem:YES];
    
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    [self configTableView];
    
    [self.view addSubview:self.addBtn];
}

- (void)configTableView {
    self.tableView.rowHeight = 44;
    [self.tableView registerClass:[HNSH_AccountCell class] forCellReuseIdentifier:HNSH_AccountCellID];
    
    HNSH_AccountHeaderView *view = [[HNSH_AccountHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 80)];
    WeakSelf(weakSelf);
    view.changeBlock = ^{
        HNSH_AccountEditViewController *vc = [[HNSH_AccountEditViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    self.headerView = view;
    self.tableView.tableHeaderView = view;
}

- (void)logout {
    WeakSelf(weakSelf);
    [HNSH_AlertView showTitle:@"是否退出登录？" des:@"" buttonTitle:@"退出" buttonColor:[UIColor appGreenColor] buttonClick:^{
        //退出登录
        [HNSH_UserNetServer requestUserLogoutSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
            
        } Failure:^(NSError * _Nullable error) {
            
        }];
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        [ST_UserManager logOut];
    }];
  
    
}

#pragma mark lazy
- (NSArray *)infoAry {
    if (!_infoAry) {
        _infoAry = @[@{@"title":@"账户与安全",@"des":@""},
//                     @{@"title":@"意见反馈",@"des":@""},
                     @{@"title":@"关于我们",@"des":[NSString stringWithFormat:@"V%@", [UIApplication sharedApplication].appVersion]}];
    }
    return _infoAry;
}

- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame = CGRectMake(16, kScreenHeight - 44 - 12 - kBottomSafeHeight, kScreenWidth - 32, 44);
        [_addBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [_addBtn setTitleColor:[UIColor appRedLabelColor] forState:UIControlStateNormal];
        _addBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _addBtn.backgroundColor = [UIColor colorWithHexString:@"#FFECEA"];
        _addBtn.layer.cornerRadius = 8;
        _addBtn.layer.masksToBounds = YES;
        [_addBtn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_AccountCellID];
    cell.dict = self.infoAry[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.infoAry[indexPath.row];
    if ([dic[@"title"] isEqualToString:@"账户与安全"]) {
        HNSH_AccountSafeViewController *vc = [[HNSH_AccountSafeViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([dic[@"title"] isEqualToString:@"意见反馈"]) {
        HNSH_FeedbackViewController *vc = [[HNSH_FeedbackViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([dic[@"title"] isEqualToString:@"关于我们"]) {
        HNSH_AboutUsViewController *vc = [[HNSH_AboutUsViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
